package com.jr.service.impl;

import com.jr.entry.Enterprise;
import com.jr.entry.Posts;
import com.jr.entry.User;
import com.jr.mapper.PostsMapper;
import com.jr.pojo.*;
import com.jr.service.PostsService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class PostsServiceImpl implements PostsService {
    @Autowired
    private PostsMapper postsMapper;

    @Autowired
    private PageMyPots pageMyPots;

    @Override
    public PageMyPots selectAllPosts(Map<String, Object> params,int page, int pageSize) {
        // 1. 先查询总记录数
        Integer totalRecords = postsMapper.countAllPosts(params);
        // 2. 计算总页数
        Integer totalPages = (int) Math.ceil((double) totalRecords / pageSize);

        // 3. 分页查询当前页数据
        RowBounds rowBounds = new RowBounds((page - 1) * pageSize, pageSize);
        ArrayList<MyPosts> posts = postsMapper.selectUserAndPost(params, rowBounds);
        // 4. 返回数据和分页信息
       pageMyPots.setList(posts);
       pageMyPots.setTotalPages(totalPages);
       pageMyPots.setTotalRecords(totalRecords);
        return pageMyPots;
    }

    @Override
    public PageMyPots selectAllPosts(String pname, int page, int pageSize) {

        // 1. 先查询总记录数
        Map<String, Object> params = new HashMap();
        params.put("pname", pname);
        Integer totalRecords = postsMapper.countAllPosts1(params);
        // 2. 计算总页数
        Integer totalPages = (int) Math.ceil((double) totalRecords / pageSize);

        // 3. 分页查询当前页数据
        RowBounds rowBounds = new RowBounds((page - 1) * pageSize, pageSize);
        ArrayList<MyPosts> posts = postsMapper.selectAllPosts(pname, rowBounds);
        // 4. 返回数据和分页信息
        pageMyPots.setList(posts);
        pageMyPots.setTotalPages(totalPages);
        pageMyPots.setTotalRecords(totalRecords);
        return pageMyPots;
    }

    @Override
    public ArrayList<HighSalInformation> selectHighSalInformation() {

        return  postsMapper.selectHighSalInformation();
    }

    @Override
    public ArrayList<MyPosts> selectUserAndPost(UserAndPos userAndPos) {
        return null;
    }

    @Override
    public ArrayList<Posts> showPosts(Posts posts) {
        return postsMapper.selectPost(posts);
    }

    @Override
    public ArrayList<EnterprisePostList> showEnterprisePostList(Enterprise enterprise) {
        return null;
    }

    @Override
    public Posts selectPotsByPname(String pname) {
        return postsMapper.selectPotsByPname(pname);
    }

    @Override
    public Boolean astatus(Map<String, Object> params) {
        return postsMapper.astatus(params);
    }

    @Override
    public Boolean addastatus(Map<String, Object> params) {
        return postsMapper.addastatus(params);
    }

    @Override
    public Integer selectId(String phone) {
        return postsMapper.selectId(phone);
    }

    @Override
    public int countAllPosts(Map<String, Object> params) {
        return 0;
    }
}
